using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NiSis.Loggin;
//using MSLogging = Microsoft.Practices.EnterpriseLibrary.Logging;

namespace NiSis
{
    /// <summary>
    /// Pagina che effettua la login dell'utente in modo automatico
    /// </summary>
    public partial class FakeLogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LogUser();
        }
        protected void LogUser()
        {
#if DEBUG
            string susr = "falco";
#else 
            string susr=GetUser();
#endif

            MembershipUser usr = Membership.GetUser(susr);

            if (!string.IsNullOrEmpty(susr))
            {
                Logger.Conferma(string.Format("Operazione: utente connesso {0}, esito: positivo", susr));
                //Conferma(string.Format("utente logato {0}", susr));
                Session.Add("nsisusr", susr);
                FormsAuthentication.RedirectFromLoginPage(susr, true);
            }
            else
                Logger.Conferma(string.Format("Operazione: login utenza {0},esito: negativo", susr));

        }
        protected string GetUser()
        {
            string susr = ConfigurationManager.AppSettings["TestUser"] ?? string.Empty;
            if (susr == string.Empty)
                susr = Request["usr"] ?? (string)Session["nsisusr"] ?? string.Empty;
            string[] mat = susr.Split('\\');
            if (mat.Length == 0)
                return string.Empty;

            return mat[mat.Length - 1];
        }

    }
}
